using System;
using System.Collections;
using System.Collections.Generic;
using Data;
using SevenPieceGame;
using TMPro;
using UnityEngine;

public class TutorialPopupHandler : MonoBehaviour, IPopupWnd, IHandlerBase
{
	public void DoDataExchange()
	{
	}

	public void OnEnterPopup(object param)
	{
		GameMode.gameMode = GameType.SQUARE;
		BlocksCoordinate.coordinate = new BlockSquareCoordinate();
		PuzzleCtrl puzzleCtrl = this.puzzleCtrl;
		puzzleCtrl.onPuzzleClear = (Action<int, List<BoardInfo>>)Delegate.Combine(puzzleCtrl.onPuzzleClear, new Action<int, List<BoardInfo>>(this.OnClear));
		this.page = ((!(bool)param) ? 1 : 0);
		this.SetLevel();
		this.SetView();
		SingletonMonoBehaviour<Director>.Instance.TopOverlayEnable(false);
	}

	public void OnLeavePopup()
	{
		PuzzleCtrl puzzleCtrl = this.puzzleCtrl;
		puzzleCtrl.onPuzzleClear = (Action<int, List<BoardInfo>>)Delegate.Remove(puzzleCtrl.onPuzzleClear, new Action<int, List<BoardInfo>>(this.OnClear));
		if (DataContainer<Data.Root>.Data.currentLevel != null)
		{
			BlocksCoordinate.coordinate.coordinate.width = DataContainer<Data.Root>.Data.currentLevel.width;
			BlocksCoordinate.coordinate.coordinate.height = DataContainer<Data.Root>.Data.currentLevel.height;
		}
		SingletonMonoBehaviour<Director>.Instance.TopOverlayEnable(true);
		
		//UserLayerManager.Instance.ChangeUserLayerByGM(UserLayerManager.UserLayerType.Install);
		if (UserLayerManager.Instance.GetCurrentUserLayer() == UserLayerManager.UserLayerType.None)
		{
			UserLayerManager.Instance.ChangeUserLayer(UserLayerManager.UserLayerType.Organic);	
		}
		else
		{
			if (UserLayerManager.Instance.IsInstallOrLowUser())
			{
				int _isFisrtGame = PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_FISRT_GAME, 0);
				if (_isFisrtGame == 0)
				{
					PlayerPrefsManager.SetInt(PlayerPrefsManager.PLAYER_FISRT_GAME, 1);
					PlayerPrefsManager.SetInt(PlayerPrefsManager.PLAYER_CURRENT_DOLLER_COUNT,0);
				}
			}
		}
		if (!UserLayerManager.Instance.IsOrganic())
		{
			if (PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_GUIDE_SHOW_ID,1002) < 1004)
			{
				MainManager.Instance.ShowUIGuide();
			}
		}
		
	}

	private void SetLevel()
	{
		this.level1 = new Level();
		this.level1.height = 2;
		this.level1.width = 3;
		this.level1.blockcount = 2;
		this.level1.num = 0;
		this.level1.level = new byte[2][];
		this.level1.level[0] = new byte[]
		{
			0,
			1,
			3
		};
		this.level1.level[1] = new byte[]
		{
			2,
			4,
			5
		};
		Level level = this.level1;
		byte[] array = new byte[2];
		array[0] = 1;
		level.index = array;
		this.level1.editMinPoint = new byte[2];
		this.level2 = new Level();
		this.level2.height = 3;
		this.level2.width = 3;
		this.level2.blockcount = 2;
		this.level2.num = 0;
		this.level2.level = new byte[3][];
		this.level2.level[0] = new byte[]
		{
			3,
			6,
			7
		};
		this.level2.level[1] = new byte[]
		{
			1,
			4,
			5,
			8
		};
		this.level2.level[2] = new byte[]
		{
			1,
			4,
			7
		};
		this.level2.index = new byte[]
		{
			1,
			0,
			2
		};
		this.level2.editMinPoint = new byte[2];
	}

	private void InitMapInfo()
	{
		BlocksCoordinate.coordinate.coordinate.width = this.level1.width;
		BlocksCoordinate.coordinate.coordinate.height = this.level1.height;
		float bannerPositionY = BlockBannerAnchor.GetBannerPositionY();
		this.bottom.transform.localPosition = new Vector3(this.bottom.transform.localPosition.x, bannerPositionY, this.bottom.transform.localPosition.z);
		this.puzzleCtrl.transform.localPosition = new Vector3(this.puzzleCtrl.transform.localPosition.x, 1f, this.puzzleCtrl.transform.localPosition.z);
	}

	private void OnClear(int move, List<BoardInfo> boardInfos)
	{
		this.curtain.SetActive(true);
		base.StartCoroutine(this.coClearPopupOpen());
		this.puzzleCtrl.ClearAnimation();
	}

	private IEnumerator coClearPopupOpen()
	{
		float clearTime = this.puzzleCtrl.ClearAnimation();
		yield return new WaitForSeconds(clearTime + 0.1f);
		this.OnClearPopup();
		yield break;
	}

	private void OnClearPopup()
	{
		this.page++;
		this.SetView();
	}

	private void SetView()
	{
		this.curtain.SetActive(false);
		if (this.page == 0)
		{
			GameMode.gameMode = GameType.SQUARE;
			BlocksCoordinate.coordinate = new BlockSquareCoordinate();
			this.desc.text = "Move block to\nfill up the board.";
			this.InitMapInfo();
			this.puzzleCtrl.LoadLevel(this.level1, true, false);
		}
		else if (this.page == 1)
		{
			GameMode.gameMode = GameType.HEXA;
			BlocksCoordinate.coordinate = new BlockHexaCoordinate();
			this.desc.text = "This Level has fake.\nBeware of the tricks of the\nextra blocks!";
			this.InitMapInfo();
			this.puzzleCtrl.LoadLevel(this.level2, true, false);
		}
		else
		{
			BlockPopupTransition.PopupAnimation(delegate
			{
				Scene.ClosePopup("TutorialPopup", null);
			});
		}
	}

	private void OnClickPlay()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		this.OnClearPopup();
	}

	private void OnClickSkip()
	{
		if (this.curtain.activeSelf)
		{
			return;
		}
		BMSoundHelper.Play("se_btnclick", false, 1f);
		BlockPopupTransition.PopupAnimation(delegate
		{
			Scene.ClosePopup("TutorialPopup", null);
		});
	}

	private void OnEscape()
	{
		this.OnClickSkip();
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public TextMeshPro desc;

	public PuzzleCtrl puzzleCtrl;

	public GameObject bottom;

	public GameObject bottomUp;

	public GameObject bottomDown;

	public GameObject initPoint;

	private Level level1;

	private Level level2;

	private int page;
}
